<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input
        v-model="listQuery.chineseName"
        placeholder="用户姓名"
        style="width: 200px;"
        class="filter-item"
        @keyup.enter.native="handleFilter"
      />
      <el-date-picker
        ref="picker"
        v-model="pickerTime"
        type="daterange"
        align="right"
        unlink-panels
        range-separator="至"
        start-placeholder="开始日期"
        end-placeholder="结束日期"
        :picker-options="pickerOptions"
        class="picker"
        @change="pickerTimeChange"
      />
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
        查询
      </el-button>
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" :disabled="disabledCreate" @click="createUser">
        创建
      </el-button>

      <a href="http://192.168.20.18:8021/api/v1/alarm/exportExcel?alarmReason=&deviceType=&pageNum=1&pageSize=10" download>下载</a>
    </div>

    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      border
      fit
      style="width: 100%;"
    >
      <el-table-column label="序号" prop="id" align="center" width="80">
        <template slot-scope="{row}">
          <span>{{ row.id }}</span>
        </template>
      </el-table-column>
      <el-table-column label="用户账号" width="100px">
        <template slot-scope="{row}">
          <span>{{ row.username }}</span>
        </template>
      </el-table-column>
      <el-table-column label="用户姓名" width="100px">
        <template slot-scope="{row}">
          <span>{{ row.chineseName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="账户状态" width="100px">
        <template slot-scope="{row}">
          <span>{{ row.dataStatusStr }}</span>
        </template>
      </el-table-column>
      <el-table-column label="联系电话" width="120px">
        <template slot-scope="{row}">
          <span>{{ row.tel }}</span>
        </template>
      </el-table-column>

      <el-table-column label="创建时间" width="160px" align="center">
        <template slot-scope="{row}">
          <span>{{ row.createDate }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建人" width="100px">
        <template slot-scope="{row}">
          <span>{{ row.creator }}</span>
        </template>
      </el-table-column>

      <el-table-column label="操作栏" align="center" class-name="small-padding fixed-width">
        <template slot-scope="{row,$index}">
          <el-button type="primary" size="mini" @click="handleUpdate(row)">
            编辑
          </el-button>
          <el-button v-if="row.status!='deleted'" size="mini" type="danger" @click="handleDelete(row,$index)">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="listQuery.pageNum"
      :limit.sync="listQuery.pageSize"
      @pagination="getList"
    />

  </div>
</template>

<script>
import waves from '@/directive/waves' // waves directive
import Pagination from '@/components/Pagination'
import { deletes, getUserByPage } from '../../../api/user' // secondary package based on el-pagination
import { computedUrlPermissions, checkPermission } from '@/utils/CommonUtils'
export default {
  name: 'ComplexTable',
  components: { Pagination },
  directives: { waves },
  data() {
    return {
      dialogFormVisible: true,
      tableKey: 0,
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        pageNum: 1,
        pageSize: 20,
        operator: '',
        chineseName: '',
        startTime: '',
        endTime: ''
      },
      importanceOptions: [1, 2, 3],
      showReviewer: false,
      downloadLoading: false,
      pickerOptions: {
        shortcuts: [{
          text: '最近一天',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近一周',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近一个月',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近三个月',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
            picker.$emit('pick', [start, end])
          }
        }]
      },
      pickerTime: '',
      disabledCreate: checkPermission('/usr/add')
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      getUserByPage(this.listQuery).then(response => {
        this.list = response.data.list
        this.total = response.data.total
        this.listLoading = false
      })
    },
    handleUpdate(row) {
      this.$bus.emit('usersDialog', { isAdd: false, isSave: true, isAddOrSave: false, successFn: this.getList, ...row })
    },
    createUser() {
      this.$bus.emit('usersDialog', { successFn: this.getList, isAddOrSave: true, isAdd: true })
    },
    handleDelete(row) {
      this.$bus.emit('conformDialog', {
        title: '提示', content: '此操作将永久删除该用户, 是否继续?', successFn: () => {
          deletes([row.id]).then(res => {
            this.$message({
              message: '删除成功！',
              type: 'success'
            })
            this.getList()
          })
        } })
    },
    handleFilter() {
      this.listQuery.pageNum = 1
      this.getList()
    },
    pickerTimeChange() {
      if (this.pickerTime) {
        this.listQuery.startTime = this.pickerTime[0].getTime()
        this.listQuery.endTime = this.pickerTime[1].getTime()
      } else {
        this.listQuery.startTime = ''
        this.listQuery.endTime = ''
      }
      this.handleFilter()
    }
  }

}
</script>

<style lang="scss" rel="stylesheet/scss" scoped>
  @import "~@/styles/variables.scss";
</style>
